package o;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.downloadengine.api.SplitTask;
import com.huawei.appgallery.foundation.application.pkgmanage.model.update.ApkUpgradeInfo;
import com.huawei.appgallery.packagemanager.api.bean.ManagerTask;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import o.cof.a;
import org.jetbrains.annotations.NotNull;

public class dpo {
    private cny ॱ;

    static final class e implements FilenameFilter {
        private e() {
        }

        public boolean accept(File file, String str) {
            if (str.startsWith("gamedl_") || str.startsWith("predl_") || str.startsWith("wishdl_")) {
                return true;
            }
            return false;
        }
    }

    public dpo() {
        fhz ˋ = fho.ˏ().ˋ("PackageManager");
        if (ˋ != null) {
            this.ॱ = (cny) ˋ.ॱ(cny.class);
        }
    }

    public void ˋ() {
        Logger.ˎ("mPreDApks", "begin ManagePreDownloadApks");
        ॱ(ContentManager.getInstant().getContext());
        cfl ˋ = epp.ˋ();
        if (ˋ != null) {
            ˋ(ˋ);
            ˏ(ˋ);
        } else {
            Logger.e("mPreDApks", "can not get appcache path");
        }
        ˏ();
    }

    private void ˋ(cfl o_cfl) {
        if (this.ॱ != null) {
            File file = new File(o_cfl.ˎ());
            if (file.isDirectory()) {
                File[] listFiles = file.listFiles(new e());
                if (listFiles != null) {
                    for (File file2 : listFiles) {
                        Object obj = null;
                        for (ManagerTask managerTask : this.ॱ.ˏ(ContentManager.getInstant().getContext())) {
                            Object obj2;
                            List<a> list = managerTask.apkInfos;
                            if (list != null) {
                                for (a aVar : list) {
                                    if (!TextUtils.isEmpty(aVar.ˎ)) {
                                        try {
                                            if (new File(aVar.ˎ).getCanonicalPath().equals(file2.getCanonicalPath())) {
                                                obj2 = 1;
                                                break;
                                            }
                                        } catch (IOException e) {
                                            Logger.ˋ("mPreDApks", "can not getCanonicalPath");
                                        }
                                    }
                                }
                            }
                            obj2 = obj;
                            obj = obj2;
                        }
                        if (obj == null) {
                            Logger.ˎ("mPreDApks", "found invalid apk file:" + file2.getName() + "," + file2.delete());
                        }
                    }
                }
            }
        }
    }

    private void ˏ(cfl o_cfl) {
        File[] listFiles = new File(o_cfl.ˎ(), "tmp").listFiles(new e());
        if (listFiles != null && listFiles.length > 0) {
            for (File file : listFiles) {
                boolean z = false;
                for (SessionDownloadTask ᐝˊ : ˎ(false)) {
                    boolean z2;
                    try {
                        if (new File(((SplitTask) ᐝˊ.ᐝˊ().get(0)).ॱˊ()).getCanonicalPath().equals(file.getCanonicalPath())) {
                            z = true;
                        }
                        z2 = z;
                    } catch (IOException e) {
                        Logger.ˋ("mPreDApks", "can not get file path");
                        z2 = z;
                    }
                    z = z2;
                }
                if (!z) {
                    Logger.ˎ("mPreDApks", "found invalid tmp file:" + file.getName() + "," + file.delete());
                }
            }
        }
    }

    private void ˏ() {
        for (SessionDownloadTask sessionDownloadTask : ˎ(true)) {
            if (sessionDownloadTask.ˌ() == 1) {
                ˊ(sessionDownloadTask);
            } else if (sessionDownloadTask.ˌ() == 3) {
                ॱ(sessionDownloadTask);
            } else if (sessionDownloadTask.ˌ() == 5) {
                ˋ(sessionDownloadTask);
            }
        }
    }

    private void ˊ(SessionDownloadTask sessionDownloadTask) {
        if (dss.ॱ().ˎ()) {
            ApkUpgradeInfo ˊ = ((cdg) cdb.ॱ(cdg.class)).ˊ(sessionDownloadTask.ˊˊ(), true, 0);
            if (ˊ == null || sessionDownloadTask.ॱᐝ() != ˊ.ʿ()) {
                Logger.ˎ("mPreDApks", "useless predownload apk,delete it-" + sessionDownloadTask.ˊˊ());
                dvt.ˏ().ॱ(sessionDownloadTask.ʾ());
                return;
            }
            return;
        }
        Logger.ˎ("mPreDApks", "auto update closed. delete it-" + sessionDownloadTask.ˊˊ());
        dvt.ˏ().ॱ(sessionDownloadTask.ʾ());
    }

    private void ॱ(SessionDownloadTask sessionDownloadTask) {
        for (ApkUpgradeInfo apkUpgradeInfo : ((cdg) cdb.ॱ(cdg.class)).ᐝ()) {
            if (sessionDownloadTask.ˊˊ().equals(apkUpgradeInfo.ᐝ())) {
                break;
            }
        }
        ApkUpgradeInfo apkUpgradeInfo2 = null;
        if (apkUpgradeInfo2 == null || apkUpgradeInfo2.ʿ() != sessionDownloadTask.ॱᐝ()) {
            Logger.ˎ("mPreDApks", "useless Game Reserved apk,delete it-" + sessionDownloadTask.ˊˊ());
            dvt.ˏ().ॱ(sessionDownloadTask.ʾ());
        }
    }

    private void ˋ(SessionDownloadTask sessionDownloadTask) {
        if (eny.ˊ().ˎ(sessionDownloadTask.ˊˊ(), sessionDownloadTask.ॱᐝ())) {
            Logger.ˎ("mPreDApks", "useless Wish Info apk,delete it-" + sessionDownloadTask.ˊˊ());
            dvt.ˏ().ॱ(sessionDownloadTask.ʾ());
        }
    }

    @NotNull
    private List<SessionDownloadTask> ˎ(boolean z) {
        List<SessionDownloadTask> arrayList = new ArrayList();
        for (SessionDownloadTask sessionDownloadTask : dvt.ˏ().ͺ()) {
            if (!((sessionDownloadTask.ˌ() != 1 && sessionDownloadTask.ˌ() != 3 && sessionDownloadTask.ˌ() != 5) || sessionDownloadTask.ᐝˊ() == null || sessionDownloadTask.ᐝˊ().isEmpty())) {
                if (z || ((SplitTask) sessionDownloadTask.ᐝˊ().get(0)).ᐝॱ() == 0) {
                    arrayList.add(sessionDownloadTask);
                    Logger.ˎ("mPreDApks", "find bg task:" + sessionDownloadTask.ˊˊ());
                }
            }
        }
        return arrayList;
    }

    private boolean ˎ(ManagerTask managerTask) {
        List list = managerTask.apkInfos;
        if (!(list == null || list.isEmpty())) {
            a aVar = (a) list.get(0);
            if (!(aVar == null || TextUtils.isEmpty(aVar.ˎ) || !new File(aVar.ˎ).getName().startsWith("predl_"))) {
                return false;
            }
        }
        return true;
    }

    private void ॱ(Context context) {
        if (this.ॱ != null) {
            List<ManagerTask> ˏ = this.ॱ.ˏ(context);
            if (ˏ != null) {
                for (ManagerTask managerTask : ˏ) {
                    boolean ˎ;
                    if (!((cdg) cdb.ॱ(cdg.class)).ˏ(managerTask.packageName, true, 0)) {
                        ˎ = ˎ(managerTask);
                    } else if (dss.ॱ().ˎ()) {
                        ˎ = true;
                    } else {
                        ˎ = ˎ(managerTask);
                    }
                    if (!ˎ) {
                        Logger.ˎ("mPreDApks", "delete useless predownload apk:" + managerTask.packageName);
                        this.ॱ.ˊ(context, managerTask.packageName);
                    }
                }
            }
        }
    }
}
